# -*- coding: utf-8 -*-
from decimal import Decimal
from future.utils import raise_with_traceback

from common import orm
from common.account.db.account import add_campaign_award_in_transaction
from common.campaign.model.winner_ranking import WinnerAwardRecord


def award_winner_ranking(user_id, award_type, rank, amount, start, end):
    try:
        extend = {
            'title': u'春节活动中奖排名奖励',
            'award_amount': amount
        }

        transaction_id = add_campaign_award_in_transaction(user_id, Decimal(amount), extend)

        record = WinnerAwardRecord()
        record.user_id = user_id
        record.start = start
        record.end = end
        record.type = award_type
        record.rank = rank
        record.amount = amount
        record.transaction_id = transaction_id
        record.save(auto_commit=False)

        orm.session.commit()
    except Exception as e:
        orm.session.rollback()
        orm.session.close()
        raise_with_traceback(e)
